<?php
namespace app\index\model;

use think\Model;
class TermModel extends Model
{
    /**
     * getTpl方法
     * @param $id 要获取的模型ID
     * @param $type 要获取的模板类型('term','content')
     * @result Str 模板名
     */
    public function getTpl($id, $type)
    {
        if(empty($id))
            return false;
        if($type != 'term'&& $type != 'content')
            return false;
        $field = $type.'_tpl';
        $where = [
            'id'    =>  $id,
        ];
        $result = $this->where($where)->field($field)->find();
        return $result[$field];
    }

    /**
     * 获取经过处理的自定义标签字段列表
     */
    public function getIdModelTags($id, $type)
    {
        if(empty($id))
            return false;
        if(!$field = $this->getTagField($type))
            return false;
        $where = [
            'id' => $id,
        ];
        $result = $this->where($where)->field($field)->find();
        $re = json_decode($result[$field], true);
        return $re;
    }

    private function getTagField($type)
    {
        //检测参数是否合法
        if($type != 'term' && $type != 'content')
            return false;
        return $field = $type.'_tags';
    }
}

